import { queryBase } from '@/api/base'
import { cropOriginData_admin, cropOriginData_manager } from '@/api/originData'
import { queryEnterprise, getAllEnterprise } from '@/api/enterprise'
import { queryGood_entId } from '@/api/good'
import { queryWarehouse_entId } from '@/api/warehouse'
import store from '@/store'

export const cropMixin = {
  data() {
    return {
      account: store.getters.token,
      role: store.getters.role,
      siteIds: [],
      ents: []
    }
  },
  methods: {
    fetchData() {
      this.listLoading = true
      if (Number.parseInt(this.role) === 1) {
        this.cropOriginData_manager()
      } else {
        queryBase({ account: this.account })
          .then(response => {
            console.log(response)
            const site = response.plantation
            this.siteIds = site.map(item => {
              return item.siteId
            })
            this.cropOriginData_admin(0)
          })
          .catch(error => {
            console.log(error)
          })
      }
      if (this.category === 'crop_produce' || this.category === 'crop_storage') {
        this.objOption()
      }
    },
    cropOriginData_manager() {
      cropOriginData_manager({ account: this.account })
        .then(async response => {
          await this.formatData(response)
          this.checkData()
          this.listLoading = false
        })
        .catch(error => {
          console.log(error)
        })
    },
    cropOriginData_admin(count) {
      if (count === this.siteIds.length) {
        this.checkData()
        this.listLoading = false
        return
      }
      cropOriginData_admin({
        siteId: this.siteIds[count]
      })
        .then(async response => {
          // console.log(response)
          await this.formatData(response)
          this.cropOriginData_admin(++count)
        })
        .catch(error => {
          console.log(error)
        })
    },
    formatData(response) {
      return new Promise(resolve => {
        switch (this.category) {
          case 'crop_type': {
            if (this.role === '1' || this.role === 1) {
              response.cropType_s.map(item => {
                if (item.length !== 0) {
                  this.cropTypeList.push(...item)
                }
              })
            } else {
              this.cropTypeList.push(...response.cropType_s)
            }
            break
          }
          case 'crop_plant': {
            let cropType = null
            if (this.role === '1' || this.role === 1) {
              response.cropType_s.map(item => {
                if (item.length !== 0) {
                  cropType = item.map(i => {
                    return {
                      label: i.name,
                      value: i.cropTypeId,
                      entId: i.entId,
                      siteId: i.siteId,
                      placeId: i.placeId
                    }
                  })
                }
              })
            } else {
              cropType = response.cropType_s.map(item => {
                return {
                  label: item.name,
                  value: item.cropTypeId,
                  entId: item.entId,
                  siteId: item.siteId,
                  placeId: item.placeId
                }
              })
            }
            this.cropTypeOptions.push(...cropType)
            this.cropPlantList.push(...response.planting)
            break
          }
          case 'crop_fertilize': {
            this.siteOption(response)
            response.fertilizer.map(item => {
              this.cropFertilizeList.push(...item)
            })
            break
          }
          case 'crop_irrigate': {
            console.log(response)
            this.siteOption(response)
            response.irrigate.map(item => {
              this.cropIrrigateList.push(...item)
            })
            break
          }
          case 'crop_handle': {
            this.siteOption(response)
            response.insectAttack.map(item => {
              this.cropHandleList.push(...item)
            })
            break
          }
          case 'crop_harvest': {
            this.siteOption(response)
            response.harvest.map(item => {
              this.cropHarvestList.push(...item)
            })
            break
          }
          case 'crop_produce': {
            this.harvestOption(response)
            response.cropProduce.map(item => {
              this.cropProduceList.push(...item)
            })
            break
          }
          case 'crop_storage': {
            // console.log(response)
            this.produceOption(response)
            response.storage.map(item => {
              this.cropStorageList.push(...item)
            })
            break
          }
        }
        resolve()
      })
    },
    checkData() {
      switch (this.category) {
        case 'crop_plant': {
          if (this.cropTypeOptions.length === 0) {
            this.cropTypeOptions.push({
              label: '请先添加作物品种',
              value: -1
            })
          }
          // this.temp.cropTypeId = this.cropTypeOptions[0].value
          // this.temp.siteId = this.cropTypeOptions[0].siteId
          // this.temp.placeId = this.cropTypeOptions[0].placceId
          break
        }
        case 'crop_fertilize':
        case 'crop_irrigate':
        case 'crop_handle':
        case 'crop_harvest': {
          if (this.cropPlantOptions.length === 0) {
            this.cropPlantOptions.push({
              label: '请先添加种植信息',
              value: -1
            })
          }
          this.temp.plantId = this.cropPlantOptions[0].value
          break
        }
        case 'crop_produce': {
          if (this.cropHarvestOptions.length === 0) {
            this.cropHarvestOptions.push({
              label: '请先添加采收信息',
              value: -1
            })
          }
          this.temp.harvestId = this.cropHarvestOptions[0].value
          break
        }
        case 'crop_storage': {
          if (this.cropProduceOptions.length === 0) {
            this.cropProduceOptions.push({
              label: '请先添加生产信息',
              value: -1
            })
          }
          this.temp.produceId = this.cropProduceOptions[0].value
          break
        }
      }
    },
    siteOption(response) {
      const cropPlant = response.planting.map(item => {
        return {
          label: item.plantName,
          value: item.plantId
        }
      })
      this.cropPlantOptions.push(...cropPlant)
    },
    harvestOption(response) {
      response.harvest.map(item => {
        item.map(i => {
          this.cropHarvestOptions.push({
            label: i.harvestName,
            value: i.harvestId
          })
        })
      })
    },
    produceOption(response) {
      response.cropProduce.map(item => {
        item.map(i => {
          this.cropProduceOptions.push({
            label: i.cropProduceName,
            value: i.cropProduceId
          })
        })
      })
    },
    objOption() {
      if (this.role === '0') {
        this.getEntAdmin()
      } else {
        this.getEnt()
      }
    },
    getEntAdmin() {
      getAllEnterprise({
        page: -1,
        limit: -1
      })
        .then(response => {
          this.formatEntData(response)
        })
        .catch(error => {
          console.log(error)
        })
    },
    getEnt() {
      queryEnterprise({
        account: this.account,
        page: -1,
        limit: -1
      })
        .then(response => {
          this.formatEntData(response)
        })
        .catch(error => {
          console.log(error)
        })
    },
    formatEntData(response) {
      console.log(response)
      this.ents = response.enterprise
      if (this.ents.length !== 0) {
        if (this.ziduan === 'good') {
          this.getGood(0)
        } else if (this.ziduan === 'warehouse') {
          this.getWarehouse(0)
        }
      }
    },
    getGood(count) {
      if (count === this.ents.length) {
        this.checkGoodOptions()
        this.listLoading = false
        return
      }
      queryGood_entId({ entId: this.ents[count].entId })
        .then(response => {
          const goods = response.good.map(item => {
            return {
              label: item.name,
              value: item.id
            }
          })
          this.goodOptions.push(...goods)
          this.getGood(++count)
        })
        .catch(error => {
          console.log(error)
        })
    },
    checkGoodOptions() {
      if (this.goodOptions.length === 0) {
        this.goodOptions.push({
          label: '请先添加商品',
          value: -1
        })
      }
      this.temp.goodId = this.goodOptions[0].value
    },
    getWarehouse(count) {
      if (count === this.ents.length) {
        this.checkWarehouseOptions()
        this.listLoading = false
        return
      }
      queryWarehouse_entId({ entId: this.ents[count].entId })
        .then(response => {
          const warehouses = response.Warehouse.map(item => {
            return {
              label: item.name,
              value: item.id
            }
          })
          this.warehouseOptions.push(...warehouses)
          this.getWarehouse(++count)
        })
        .catch(error => {
          console.log(error)
        })
    },
    checkWarehouseOptions() {
      if (this.warehouseOptions.length === 0) {
        this.warehouseOptions.push({
          label: '请先添加仓库',
          value: -1
        })
      }
      this.temp.warehouseId = this.warehouseOptions[0].value
    }
  }
}
